/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GISPackage;

import LabelPanelPackage.LabelPanel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.util.Vector;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 *
 * @author Heather
 */
public class CCPVGISPanelWithLegend extends JPanel
{

  /**
   * @return the DEFAULT_LEGEND_SIZE
   */
  public static Dimension getDEFAULT_LEGEND_SIZE() {
    return DEFAULT_LEGEND_SIZE;
  }

  /**
   * @return the DEFAULT_LEGEND_LOCATION
   */
  public static Point getDEFAULT_LEGEND_LOCATION() {
    return DEFAULT_LEGEND_LOCATION;
  }

  /**
   * @return the DEFAULT_LEGEND_COLOR
   */
  public static Color getDEFAULT_LEGEND_COLOR() {
    return DEFAULT_LEGEND_COLOR;
  }

  private CCPVBasicGISPanel GIS = new CCPVBasicGISPanel();
  // legend variables
  private LabelPanel lp = new LabelPanel();
  private JScrollPane legendContainer = new JScrollPane();
  private static final Dimension DEFAULT_LEGEND_SIZE = new Dimension(240, 300);
  private static final Point DEFAULT_LEGEND_LOCATION = new Point(20, 20);
  private static final Color DEFAULT_LEGEND_COLOR = Color.white;
  private Color regionLabelHighlightColor = new Color(110, 250, 110, 255);
  private Color lpBackground = Color.white;
  private Color scrollPaneBackground = Color.white;
  private Color labelBackground = Color.white;

  public CCPVGISPanelWithLegend()
  {
    setLayout(null);
    GIS.setBounds(0, 0, 1500, 900);
    setupLegend();
    add(GIS);

    addListeners();
  }

  private void setupLegend()
  {
    legendContainer.setSize(DEFAULT_LEGEND_SIZE);
    legendContainer.setPreferredSize(DEFAULT_LEGEND_SIZE);
    legendContainer.setBackground(scrollPaneBackground);

    add(legendContainer);
    legendContainer.setLocation(DEFAULT_LEGEND_LOCATION);
    legendContainer.setBackground(DEFAULT_LEGEND_COLOR);

    legendContainer.setViewportView(lp);
    lp.setSize(DEFAULT_LEGEND_SIZE);
    lp.setPreferredSize(DEFAULT_LEGEND_SIZE);
    lp.setBackground(lpBackground);
  }

  private void setupLabels()
  {
    System.out.println("setup labels called");
    lp.removeAll();
    Vector<Region> regions = GIS.getRegions().regions;

    for (final Region r : regions)
    {
      final JLabel regionL = new JLabel(r.name);
      lp.addLabel(regionL);
      regionL.setBackground(labelBackground);

      regionL.addMouseListener(new java.awt.event.MouseAdapter()
      {

        public void mouseClicked(java.awt.event.MouseEvent evt)
        {
        }

        public void mouseEntered(java.awt.event.MouseEvent evt)
        {
          System.out.println("FIRED!");
          Vector<Region> currentSelectedRegions = getGIS().getSelectedRegions();
          currentSelectedRegions.clear();
          currentSelectedRegions.add(r);
          System.out.println("Before: " + regionL.getBackground());
          regionL.setBackground(getRegionLabelHighlightColor());
          System.out.println("After:  " + regionL.getBackground());
          System.out.println(regionL.getSize());

          Component[] allLabels = getLp().getComponents();
          getLp().removeAll();
          for (Component c : allLabels)
          {
            getLp().add(c);
          }

          regionL.revalidate();
          regionL.updateUI();
          regionL.repaint();
          getLegendContainer().revalidate();
          getLegendContainer().updateUI();
          getLegendContainer().repaint();
          getLp().revalidate();
          getLp().repaint();
          getLp().updateUI();
        }

        public void mouseExited(java.awt.event.MouseEvent evt)
        {
          //regionL.setBackground(labelBackground);
          repaint();
        }

        public void mousePressed(java.awt.event.MouseEvent evt)
        {
        }

        public void mouseReleased(java.awt.event.MouseEvent evt)
        {
        }
      });
    }

    updateUI();
    repaint();
  }

  private void addListeners()
  {
    GIS.addMouseListener(new java.awt.event.MouseAdapter()
    {

      public void mouseClicked(java.awt.event.MouseEvent evt)
      {
        setupLabels();
      }

      public void mouseEntered(java.awt.event.MouseEvent evt)
      {
      }

      public void mouseExited(java.awt.event.MouseEvent evt)
      {
      }

      public void mousePressed(java.awt.event.MouseEvent evt)
      {
      }

      public void mouseReleased(java.awt.event.MouseEvent evt)
      {
      }
    });

    GIS.getCreateNewRegionDone().addPropertyChangeListener(new java.beans.PropertyChangeListener()
    {

      public void propertyChange(java.beans.PropertyChangeEvent evt)
      {
        setupLabels();
      }
    });
  }

  public void readXmlFile(String fileName)
  {
    GIS.readXmlFile(fileName);
  }

  /**
   * @return the gis
   */
  public CCPVBasicGISPanel getGIS() {
    return GIS;
  }

  /**
   * @return the lp
   */
  public LabelPanel getLp() {
    return lp;
  }

  /**
   * @return the legendContainer
   */
  public JScrollPane getLegendContainer() {
    return legendContainer;
  }

  /**
   * @return the regionLabelHighlightColor
   */
  public Color getRegionLabelHighlightColor() {
    return regionLabelHighlightColor;
  }

  /**
   * @return the lpBackground
   */
  public Color getLpBackground() {
    return lpBackground;
  }

  /**
   * @return the scrollPaneBackground
   */
  public Color getScrollPaneBackground() {
    return scrollPaneBackground;
  }

  /**
   * @return the labelBackground
   */
  public Color getLabelBackground() {
    return labelBackground;
  }

  public Vector<GISMenuItem> getAllRegionMenuItems()
  {
    return GIS.getAllRegionMenuItems();
  }
}
